import axios from 'axios'
import mock from '../mock/index'

const instance = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  // withCredentials: false,
  headers: {
    // 'content-type': 'application/json;charset=utf-8'
  },
  timeout: 5000
})

if (process.env.NODE_ENV === 'development'){
  mock.bindMockResponse(instance);
}
// http request 拦截器
instance.interceptors.request.use(
  config => {
    if (process.env.VUE_APP_JWT_ENABLE) {
      if (localStorage) {
        const token = localStorage.getItem(process.env.VUE_APP_JWT_COOKIE_NAME)
        if (token) {
          config.headers[process.env.VUE_APP_JWT_AUTHORIZATION_KEY] = `${process.env.VUE_APP_JWT_AUTHORIZATION_PREFIX} ${token}`
        }
      }
    }
    return config
  },
  err => {
    // Message.error({
    //   content: '服务器请求超时，请稍后重试.',
    //   duration: 5
    // })
    return Promise.reject(err)
  })

export default {
  install(app){
    app.config.globalProperties.$http = instance;
  },
};
